
#include "stdafx.h"
#include "imagectrl.h"
#include "global.h"

ImageCtrl::ImageCtrl(void)
	: m_normalimage(NULL)
	, m_hoverimage(NULL)
	, m_downimage(NULL)
	, m_callback(NULL)
	, m_status(status_normal)
{
}

ImageCtrl::~ImageCtrl(void)
{
}

void ImageCtrl::Load(const WCHAR* normal_imgfilename, const WCHAR* hover_imgfilename, const WCHAR* down_imgfilename)
{
	LoadFromFile(normal_imgfilename, &m_normalimage);
	LoadFromFile(hover_imgfilename, &m_hoverimage);
	LoadFromFile(down_imgfilename, &m_downimage);
}

void ImageCtrl::Load(const ResInfo* normal, const ResInfo* hover, const ResInfo* down)
{
	LoadFromRes(normal, &m_normalimage);
	LoadFromRes(hover, &m_hoverimage);
	LoadFromRes(down, &m_downimage);
}

void ImageCtrl::SetRect(const RECT& rect)
{
	m_rect = rect;
}

void ImageCtrl::SetCallback(Callback* callback)
{
	m_callback = callback;
}

RECT ImageCtrl::GetRect()
{
	return m_rect;
}

SIZE ImageCtrl::GetImageSize()
{
	CxImage* image = NULL;
	SIZE ret = {0, 0};

	if (m_normalimage != NULL)
		image = m_normalimage;
	else if (m_hoverimage != NULL)
		image = m_hoverimage;
	else if (m_downimage != NULL)
		image = m_downimage;

	if (image == NULL)
		return ret;
	ret.cx = image->GetWidth();
	ret.cy = image->GetHeight();
	return ret;
}

void ImageCtrl::Draw(HDC dc)
{
	CxImage* image = NULL;

	switch (m_status)
	{
	case status_normal:
		image = m_normalimage;
		break;
	case status_hover:
		image = m_hoverimage;
		break;
	case status_down:
		image = m_downimage;
		break;
	}

	if (image == NULL)
	{
		if (m_normalimage != NULL)
			image = m_normalimage;
		else if (m_hoverimage != NULL)
			image = m_hoverimage;
		else if (m_downimage != NULL)
			image = m_downimage;
	}

	if (image != NULL)
		image->Draw(dc, m_rect);
}

bool ImageCtrl::MouseMove(const POINT& point)
{
	bool ret = false;
	Status status = status_normal;

	if (PtInRect(&m_rect, point))
	{
		status = status_hover;
		ret = true;
	}

	if (m_status != status)
	{
		m_status = status;
		ForceDraw();
	}

	return ret;
}

bool ImageCtrl::MouseDown(const POINT& point)
{
	bool ret = false;
	Status status = status_normal;

	if (PtInRect(&m_rect, point))
	{
		status = status_down;
		ret = true;
	}

	if (m_status != status)
	{
		m_status = status;
		ForceDraw();
	}

	return ret;
}

bool ImageCtrl::MouseUp(const POINT& point)
{
	bool ret = false;
	Status status = status_normal;

	if (PtInRect(&m_rect, point))
	{
		status = status_hover;
		if (m_callback != NULL)
			m_callback->OnClick(this);
		ret = true;
	}

	if (m_status != status)
	{
		m_status = status;
		ForceDraw();
	}

	return ret;
}

void ImageCtrl::LoadFromFile(const WCHAR* filename, CxImage** image)
{
	if ((*image) != NULL)
	{
		delete (*image);
		(*image) = NULL;
	}

	if (filename != NULL)
	{
		(*image) = new CxImage();
		(*image)->Load(filename);
	}
}

void ImageCtrl::LoadFromRes(const ResInfo* resinfo, CxImage** image)
{
	if ((*image) != NULL)
	{
		delete (*image);
		(*image) = NULL;
	}

	if (resinfo != NULL)
	{
		(*image) = new CxImage();
		(*image)->LoadResource(FindResourceW(NULL, MAKEINTRESOURCE(resinfo->resid), resinfo->type), resinfo->format);
	}
}

void ImageCtrl::ForceDraw()
{
	HDC dc = GetDC(Global::instance().hwnd);
	Draw(dc);
	ReleaseDC(Global::instance().hwnd, dc);
	dc = NULL;
}
